package com.example.customview.drawable

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.Paint.FontMetrics
import android.graphics.PixelFormat
import android.graphics.drawable.Drawable
import com.android.baselibrary.utils.dp2px
import com.example.customview.R

class MedalDrawable constructor(val context:Context) : Drawable() {
    private var mNumberPaint:Paint = Paint()
    private var mTextPaint:Paint = Paint()
    private lateinit var mBitmap:Bitmap
    private var mStrokePaint :Paint = Paint()

    private var mTextSize:Float = dp2px(7).toFloat()

    private var mText = "荣耀王者"
    private var mNumber = "20"

    private  lateinit var mTextFontMetrics:FontMetrics
    private lateinit var mNumberFontMetrics: FontMetrics

    init {
        initBitmap()
        initNumberPaint()
        initTextPaint()
    }

    private fun initBitmap() {
        mBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.medal)

    }

    private fun initTextPaint() {
        mTextPaint.run {
            color = Color.WHITE
            isFakeBoldText = true
            isAntiAlias = true
            textSize = mTextSize
        }
        mNumberFontMetrics = mTextPaint.fontMetrics


    }

    private fun initNumberPaint() {
        mNumberPaint.run {
            color = Color.WHITE
            isFakeBoldText = true
            isAntiAlias = true
         //   getTextBounds(mNumber,0,mNumber.length, Rect())
        }
        mStrokePaint.run {
            color = Color.parseColor("#bf11e6")
            isFakeBoldText = true
            isAntiAlias = true
            strokeWidth = 4f
            style = Paint.Style.STROKE
           // getTextBounds(mNumber,0,mNumber.length, Rect())
        }
        if (mNumber.length>2){
            mNumberPaint.textSize = 16f
            mStrokePaint.textSize = 16f
        }else{
            mNumberPaint.textSize = 19f
            mStrokePaint.textSize = 19f
        }
        mTextFontMetrics = mTextPaint.fontMetrics


    }

    override fun getIntrinsicHeight(): Int {
        return  mBitmap.height
    }

    override fun getIntrinsicWidth(): Int {
        return mBitmap.width
    }


    //绘制方法
    override fun draw(canvas: Canvas) {
        canvas.drawBitmap(mBitmap,0f,0f,null)
        val baseLine = intrinsicHeight/2+(mTextFontMetrics.bottom-mTextFontMetrics.top)/2-mTextFontMetrics.bottom
        canvas.drawText(mText, dp2px(20).toFloat(),baseLine,mTextPaint)

        val baseLineN = intrinsicHeight/2+(mNumberFontMetrics.bottom-mNumberFontMetrics.top)/2-mNumberFontMetrics.bottom
        canvas.drawText(mNumber,18f,baseLineN,mStrokePaint)
        canvas.drawText(mNumber,18f,baseLineN,mNumberPaint)


    }
    //设置drawable的透明度
    override fun setAlpha(alpha: Int) {
    }
    //设置颜色过滤器
    override fun setColorFilter(colorFilter: ColorFilter?) {
    }
    //获取不透明度
    override fun getOpacity(): Int {
        return  PixelFormat.TRANSPARENT
    }


}